package com.calvin.springcloud.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.calvin.springcloud.base.ResponseResult;
import com.calvin.springcloud.model.user.User;

@RestController
public class ConsumerUserController {

	/**
	 * 理解：消费者，不应该有service层~ 提供多种便捷访问远程http服务的方法，简单的Restful服务模板~
	 */
	@Autowired
	private RestTemplate restTemplate;

	private static final String REST_URL_PREFIX = "http://localhost:8001";

	private static final String eureka_server_url = "http://PROVIDER-USER";

	
	@RequestMapping("/consumer/user/getUserList")
	public ResponseResult<Object> getUserList() {
		ResponseResult<Object> resp = restTemplate.getForObject(eureka_server_url + "/user/getUserList", ResponseResult.class);
		return resp;
	}
	
	@GetMapping("/consumerUser/findUser/{userId}/{userName}")
	public ResponseResult<User> findUser(@PathVariable Long userId, @PathVariable String userName) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("userId", userId);
		paramMap.put("userName", userName);
		ResponseResult<User> user = restTemplate.getForObject(
				eureka_server_url + "/user/findUser/{userId}/{userName}", ResponseResult.class,
				paramMap);
		return user;
	}

}
